package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.DateTimePickDialogUtil;
import com.bocai.eelly.utils.DialogUtil;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.TimeUtils;
import com.bocai.eelly.utils.Utils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 发布辅料询价
 */
public class PostAccessoriesInquiryActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_post_fuliao_name)
    EditText etPostFuliaoName;
    @Bind(R.id.et_post_fuliao_model)
    EditText etPostFuliaoModel;
    @Bind(R.id.et_post_fuliao_specifications)
    EditText etPostFuliaoSpecifications;
    @Bind(R.id.tv_post_fuliao_brand)
    TextView tvPostFuliaoBrand;
    @Bind(R.id.ll_post_fuliao_brand)
    LinearLayout llPostFuliaoBrand;
    @Bind(R.id.et_post_fuliao_color)
    EditText etPostFuliaoColor;
    @Bind(R.id.tv_post_fuliao_use)
    TextView tvPostFuliaoUse;
    @Bind(R.id.ll_post_fuliao_use)
    LinearLayout llPostFuliaoUse;
    @Bind(R.id.et_post_fuliao_num)
    EditText etPostFuliaoNum;
    @Bind(R.id.tv_post_fuliao_plantime)
    TextView tvPostFuliaoPlantime;
    @Bind(R.id.ll_post_fuliao_plantime)
    LinearLayout llPostFuliaoPlantime;
    @Bind(R.id.tv_post_fuliao_validity_period)
    TextView tvPostFuliaoValidityPeriod;
    @Bind(R.id.ll_post_fuliao_validity_period)
    LinearLayout llPostFuliaoValidityPeriod;
    @Bind(R.id.et_post_fuliao_note)
    EditText etPostFuliaoNote;
    @Bind(R.id.ll_post_fuliao_choose_photos)
    LinearLayout llPostFuliaoChoosePhotos;
    @Bind(R.id.tv_post_fuliao_commit)
    TextView tvPostFuliaoCommit;
    @Bind(R.id.tv_post_fuliao_num)
    TextView tvPostFuliaoNum;
    private ArrayList<String> photos = new ArrayList<>();
    private File[] files;
    private String[] fileKeys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_accessories_inquiry);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_post_inquiry, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initEvent() {
        llPostFuliaoBrand.setOnClickListener(this);
        llPostFuliaoUse.setOnClickListener(this);
        llPostFuliaoValidityPeriod.setOnClickListener(this);
        llPostFuliaoPlantime.setOnClickListener(this);
        llPostFuliaoChoosePhotos.setOnClickListener(this);
        tvPostFuliaoCommit.setOnClickListener(this);
        tvPostFuliaoNum.setOnClickListener(this);
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_post_fuliao_num:
                List<String> nums = new ArrayList<>();
                String[] num = {"个", "件"};
                for (int i = 0; i < num.length; i++) {
                    nums.add(num[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, nums), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFuliaoNum.setText(item.toString());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.ll_post_fuliao_brand://品牌
                List<String> brands = new ArrayList<>();
                String[] brand = {"YKK", "IDEAL", "SBS", "万美", "其它"};
                for (int i = 0; i < brand.length; i++) {
                    brands.add(brand[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, brands), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFuliaoBrand.setText(item.toString());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.ll_post_fuliao_use://用途
                List<String> uses = new ArrayList<>();
                String[] use = {"大货用", "样衣用"};
                for (int i = 0; i < use.length; i++) {
                    uses.add(use[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, uses), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFuliaoUse.setText(item.toString());
                        dialog.dismiss();
                    }
                });
                break;

            case R.id.ll_post_fuliao_validity_period://有效期
                new DateTimePickDialogUtil(this).dateTimePicKDialog(tvPostFuliaoValidityPeriod);
                break;
            case R.id.ll_post_fuliao_plantime://要求货期
                new DateTimePickDialogUtil(this).dateTimePicKDialog(tvPostFuliaoPlantime);
                break;
            case R.id.ll_post_fuliao_choose_photos:
                Intent intent = new Intent(this, ChoosePhotosActivity.class);
                intent.putExtra("photos", photos);
                startActivity(intent);
                break;
            case R.id.tv_post_fuliao_commit:
                if (files == null || files.length == 0) {
                    T.showShort(this, "请选择照片");
                } else {
                    //要求货期
                    String requirement_delivery = tvPostFuliaoPlantime.getText().toString();
                    L.d("要求货期", requirement_delivery);
                    //有效期
                    String validity_period = tvPostFuliaoValidityPeriod.getText().toString();
                    L.d("有效期", validity_period);
                    String name = etPostFuliaoName.getText().toString();
                    String model = etPostFuliaoModel.getText().toString();
                    String number = etPostFuliaoNum.getText().toString();
                    String specifications = etPostFuliaoSpecifications.getText().toString();
                    String remarks = etPostFuliaoNote.getText().toString();
                    String brandname = tvPostFuliaoBrand.getText().toString();
                    String color = etPostFuliaoColor.getText().toString();
                    String purpose = tvPostFuliaoUse.getText().toString();
                    if (Utils.isEmpty(name)) {
                        T.showShort(this, "请输入辅料料名称");
                    } else if (Utils.isEmpty(model)) {
                        T.showShort(this, "请输入型号");
                    } else if (Utils.isEmpty(number)) {
                        T.showShort(this, "请输入产品数量");
                    } else if (Utils.isEmpty(specifications)) {
                        T.showShort(this, "请输入规格");
                    } else if (Utils.isEmpty(brandname)) {
                        T.showShort(this, "请输入品牌");
                    } else if (Utils.isEmpty(color)) {
                        T.showShort(this, "请输入颜色名称");
                    } else if (Utils.isEmpty(purpose)) {
                        T.showShort(this, "请输入产品用途");
                    } else if (Utils.isEmpty(requirement_delivery)) {
                        T.showShort(this, "请输入要求货期");
                    } else if (Utils.isEmpty(validity_period)) {
                        T.showShort(this, "请输入询价有效期");
                    } else {
                        Loading.show(this, getString(R.string.str_loading));
                        commit();
                    }
                }

                break;
        }
    }

    private void commit() {
        //要求货期
        String requirement_delivery = TimeUtils.dataOne(tvPostFuliaoPlantime.getText().toString());
        L.d("要求货期", requirement_delivery);
        //有效期
        String validity_period = TimeUtils.dataOne(tvPostFuliaoValidityPeriod.getText().toString());
        L.d("有效期", validity_period);
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_FULIAO_INQUIRY, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    T.showShort(PostAccessoriesInquiryActivity.this,e.toString());
                    Loading.dismiss();
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    T.showShort(PostAccessoriesInquiryActivity.this, response);
                    L.e("辅料询价", response);

                }
            }, files, fileKeys, getParams(etPostFuliaoName.getText().toString(), etPostFuliaoModel.getText().toString(), etPostFuliaoNum.getText().toString(),
                    tvPostFuliaoNum.getText().toString(), etPostFuliaoSpecifications.getText().toString(), etPostFuliaoNote.getText().toString(), tvPostFuliaoBrand.getText().toString(),
                    etPostFuliaoColor.getText().toString(), tvPostFuliaoUse.getText().toString(), requirement_delivery, validity_period));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public OkHttpClientManager.Param[] getParams(String name, String model, String number, String number_un, String specifications, String remarks, String brand
            , String color, String purpose, String requirement_delivery, String validity_period) {
        String timeline = Utils.getTimeline();
        String sign = Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(this, "id")),
                new OkHttpClientManager.Param("name", name),
                new OkHttpClientManager.Param("model", model),
                new OkHttpClientManager.Param("number", number),
                new OkHttpClientManager.Param("number_un", number_un),
                new OkHttpClientManager.Param("specifications", specifications),
                new OkHttpClientManager.Param("remarks", remarks),
                new OkHttpClientManager.Param("brand", brand),
                new OkHttpClientManager.Param("color", color),
                new OkHttpClientManager.Param("purpose", purpose),
                new OkHttpClientManager.Param("requirement_delivery", requirement_delivery),
                new OkHttpClientManager.Param("validity_period", validity_period),};
    }

}
